import Vue from "vue";
import Vuex from "vuex";
import order from "./order.js";

Vue.use(Vuex);

export default new Vuex.Store({
	state: {
		// 加入订单数据
		joinOrderData: [],
		date: [
			//收藏加关注
		],
	},

	getters: {},
	mutations: {
		add(state, list) {
			for (var i = 0; i < state.date.length; i++) {
				if (state.date[i].actorIdUrl == list.actorIdUrl) {
					state.date[i] = list;
					return;
				}
			}
			state.date.push(list);
		},
		addData(state, arr) {
			console.log("arr", arr);
			let same = [];
			if (state.joinOrderData.length == 0) {
				state.joinOrderData.push(...arr);
			} else {
				if (arr.length > state.joinOrderData.length) {
					for (let i = 0; i < arr.length; i++) {
						//判断是否有相同商品
						if (i == state.joinOrderData.length) {
							//判断same是否有数据
							if (same.length != 0) {
								for (let j = 0; j < same.length; j++) {
									arr.splice(same[j], 1);
								}
							}
							//判断arr是否有数据
							if (arr.length != 0) {
								state.joinOrderData.push(...arr);
							}
							return;
						} else if (arr[i].id == state.joinOrderData[i].id) {
							state.joinOrderData[i].num += 1;
							same.push(i);
						}
					}
				} else if (arr.length == state.joinOrderData.length) {
					for (let i = 0; i < arr.length; i++) {
						//判断是否有相同商品
						if (i == state.joinOrderData.length - 1) {
							//判断same是否有数据
							if (same.length != 0) {
								for (let j = 0; j < same.length; j++) {
									arr.splice(same[j], 1);
								}
							}
							//判断arr是否有数据
							if (arr.length != 0) {
								state.joinOrderData.push(...arr);
							}
							return;
						} else if (arr[i].id == state.joinOrderData[i].id) {
							state.joinOrderData[i].num += 1;
							same.push(i);
						}
					}
				} else {
					for (let i = 0; i < state.joinOrderData.length; i++) {
						//判断是否有相同商品
						if (i == arr.length) {
							//判断same是否有数据
							if (same.length != 0) {
								for (let j = 0; j < same.length; j++) {
									arr.splice(same[j], 1);
								}
							}
							//判断arr是否有数据
							if (arr.length != 0) {
								state.joinOrderData.push(...arr);
							}
							return;
						} else if (arr[i].id == state.joinOrderData[i].id) {
							state.joinOrderData[i].num += 1;
							same.push(i);
						}
					}
				}
			}
			console.log("state.joinOrderData", state.joinOrderData);
		},
		//关注，取消关注
		attentionChange(state, i) {
			state.date[i].follow = !state.date[i].follow;
		}
	},
	actions: {},
	modules: {
		order,
	},
});
